package com.edu.controller;

import com.edu.bean.Book;
import com.edu.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author 75099637@qq.com
 * @PackageName SpringAndMybatis
 * @Package com.edu.controller
 * @Date 2023/11/15 14:06
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/books")
public class BookController {
    @Resource
    private BookService bookService;

    @RequestMapping(value = "/selectBookAll")
    public String selectBookAll(int pageNum, Model model, HttpSession session){
        if (session.getAttribute("loginUser") == null){
            model.addAttribute("msg", "请登录");
            return "/user/login";
        }
        int pageSize = 10;
        List<Book> bookList = bookService.getBookByPage(pageNum, pageSize);
        int pageCount = bookService.getPageCount(pageSize);
        model.addAttribute("bookList", bookList);
        model.addAttribute("pageCount", pageCount);
        model.addAttribute("pageNum", pageNum);
        return "/book/book";
    }
    @RequestMapping(value = "/{bookId}", method = RequestMethod.GET)
    public String selectBookId(@PathVariable("bookId") int id, Model model, HttpSession session) {
        if (session.getAttribute("loginUser") == null){
            model.addAttribute("msg", "请登录");
            return "/user/login";
        }
        Book book = bookService.getBookById(id);
        model.addAttribute("book", book);
        return "/book/bookDetail";
    }
    @RequestMapping(value = "/selectUpdateBookId/{bookId}", method = RequestMethod.GET)
    public String selectUpdateBookId(@PathVariable("bookId") int id, Model model, HttpSession session){
        if (session.getAttribute("loginUser") == null){
            model.addAttribute("msg", "请登录");
            return "/user/login";
        }
        Book book = bookService.getBookById(id);
        model.addAttribute("book", book);
        return "/book/bookUpdate";
    }
    @RequestMapping(value = "/updateBook", method = RequestMethod.GET)
    public String updateBook(Model model,Book book, HttpSession session){
        if (session.getAttribute("loginUser") == null){
            model.addAttribute("msg", "请登录");
            return "/user/login";
        }
        int count = bookService.updateBook(book);
        if (count == 1) {
            model.addAttribute("msg","修改成功");
            return "forward:/books/selectBookAll?pageNum=1";
        }else{
            model.addAttribute("msg","修改失败");
            return "/book/bookUpdate";
        }
    }
    @RequestMapping(value = "/deleteBook/{bookId}", method = RequestMethod.GET)
    public String deleteBook(Model model, @PathVariable(value = "bookId") int bookId, HttpSession session){
        if (session.getAttribute("loginUser") == null){
            model.addAttribute("msg", "请登录");
            return "/user/login";
        }
        int count = bookService.delBook(bookId);
        if (count > 0) {
            model.addAttribute("msg","删除成功");
            return "forward:/books/selectBookAll?pageNum=1";
        }else{
            model.addAttribute("msg","未找到带有"+bookId+"的书");
            return "forward:/books/selectBookAll?pageNum=1";
        }

    }

}
